#include "motor.h"

my_motor::my_motor(int motorPin_1, int motorPin_2) : motorPin_1(motorPin_1), motorPin_2(motorPin_2)
{
    pinMode(motorPin_1,OUTPUT);
    pinMode(motorPin_2,OUTPUT);
    motorRun(0); //电机初始化时速度为0
}
//motorRun函数输入为-255~255的摸拟量
void my_motor::motorRun(int speed)
{
    if (speed < 5 && speed > -5)
    {
        digitalWrite(motorPin_2, LOW);
        digitalWrite(motorPin_1, LOW);
    }else{
      
      if (speed < 0)                                          //如果参数为负，即电机反向运动
      {
          speed = -speed;
          speed = (speed > MAX_SPEED) ? MAX_SPEED : speed;    //三元运算符，Arduino的模拟量输出值不能超过255
          digitalWrite(motorPin_1, HIGH);
          digitalWrite(motorPin_2, LOW);
          //analogWrite(motorPin_1, speed);                     //摸拟输出
          //analogWrite(motorPin_2, 0);
      }
      else if(speed > 0)                                                    //如果参数为正，即电机正向运动
      {
          speed = (speed > MAX_SPEED) ? MAX_SPEED : speed;
          digitalWrite(motorPin_2, HIGH);
          digitalWrite(motorPin_1, LOW);
          //analogWrite(motorPin_1, 0);
          //analogWrite(motorPin_2, speed);
      }
      
      }

}
